using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class ClientCompany
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("CompanyID", "HL_ClientCompany");
		}
		public bool Exists(int CompanyID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_ClientCompany");
			stringBuilder.Append(" where CompanyID=@CompanyID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CompanyID", SqlDbType.Int, 4)
			};
			array[0].Value = CompanyID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.ClientCompany model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_ClientCompany(");
			stringBuilder.Append("ClientID,CompanyName,Country,Province,City,Address,ZipCode,Phone,Fax,Homepage,BankOfDeposit,BankAccount,TaxNum,StatusInField,CompanySize,BusinessScope,AnnualSales,ManagementForms,RegisteredCapital,CompanyIntro,CompanyPic,Districts)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@ClientID,@CompanyName,@Country,@Province,@City,@Address,@ZipCode,@Phone,@Fax,@Homepage,@BankOfDeposit,@BankAccount,@TaxNum,@StatusInField,@CompanySize,@BusinessScope,@AnnualSales,@ManagementForms,@RegisteredCapital,@CompanyIntro,@CompanyPic,@Districts)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4),
				new SqlParameter("@CompanyName", SqlDbType.NVarChar, 200),
				new SqlParameter("@Country", SqlDbType.NVarChar, 50),
				new SqlParameter("@Province", SqlDbType.NVarChar, 30),
				new SqlParameter("@City", SqlDbType.NVarChar, 30),
				new SqlParameter("@Address", SqlDbType.NVarChar, 255),
				new SqlParameter("@ZipCode", SqlDbType.NVarChar, 10),
				new SqlParameter("@Phone", SqlDbType.NVarChar, 50),
				new SqlParameter("@Fax", SqlDbType.NVarChar, 30),
				new SqlParameter("@Homepage", SqlDbType.NVarChar, 255),
				new SqlParameter("@BankOfDeposit", SqlDbType.NVarChar, 50),
				new SqlParameter("@BankAccount", SqlDbType.NVarChar, 50),
				new SqlParameter("@TaxNum", SqlDbType.NVarChar, 50),
				new SqlParameter("@StatusInField", SqlDbType.NVarChar, 30),
				new SqlParameter("@CompanySize", SqlDbType.NVarChar, 30),
				new SqlParameter("@BusinessScope", SqlDbType.NVarChar, 200),
				new SqlParameter("@AnnualSales", SqlDbType.NVarChar, 50),
				new SqlParameter("@ManagementForms", SqlDbType.NVarChar, 30),
				new SqlParameter("@RegisteredCapital", SqlDbType.NVarChar, 50),
				new SqlParameter("@CompanyIntro", SqlDbType.NText),
				new SqlParameter("@CompanyPic", SqlDbType.NVarChar, 255),
				new SqlParameter("@Districts", SqlDbType.NVarChar, 100)
			};
			array[0].Value = model.ClientID;
			array[1].Value = model.CompanyName;
			array[2].Value = model.Country;
			array[3].Value = model.Province;
			array[4].Value = model.City;
			array[5].Value = model.Address;
			array[6].Value = model.ZipCode;
			array[7].Value = model.Phone;
			array[8].Value = model.Fax;
			array[9].Value = model.Homepage;
			array[10].Value = model.BankOfDeposit;
			array[11].Value = model.BankAccount;
			array[12].Value = model.TaxNum;
			array[13].Value = model.StatusInField;
			array[14].Value = model.CompanySize;
			array[15].Value = model.BusinessScope;
			array[16].Value = model.AnnualSales;
			array[17].Value = model.ManagementForms;
			array[18].Value = model.RegisteredCapital;
			array[19].Value = model.CompanyIntro;
			array[20].Value = model.CompanyPic;
			array[21].Value = model.Districts;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.ClientCompany model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_ClientCompany set ");
			stringBuilder.Append("ClientID=@ClientID,");
			stringBuilder.Append("CompanyName=@CompanyName,");
			stringBuilder.Append("Country=@Country,");
			stringBuilder.Append("Province=@Province,");
			stringBuilder.Append("City=@City,");
			if (!string.IsNullOrEmpty(model.Districts))
			{
				stringBuilder.Append("Districts='" + model.Districts + "',");
			}
			stringBuilder.Append("Address=@Address,");
			stringBuilder.Append("ZipCode=@ZipCode,");
			stringBuilder.Append("Phone=@Phone,");
			stringBuilder.Append("Fax=@Fax,");
			stringBuilder.Append("Homepage=@Homepage,");
			stringBuilder.Append("BankOfDeposit=@BankOfDeposit,");
			stringBuilder.Append("BankAccount=@BankAccount,");
			stringBuilder.Append("TaxNum=@TaxNum,");
			stringBuilder.Append("StatusInField=@StatusInField,");
			stringBuilder.Append("CompanySize=@CompanySize,");
			stringBuilder.Append("BusinessScope=@BusinessScope,");
			stringBuilder.Append("AnnualSales=@AnnualSales,");
			stringBuilder.Append("ManagementForms=@ManagementForms,");
			stringBuilder.Append("RegisteredCapital=@RegisteredCapital,");
			stringBuilder.Append("CompanyIntro=@CompanyIntro,");
			stringBuilder.Append("CompanyPic=@CompanyPic");
			stringBuilder.Append(" where CompanyID=@CompanyID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4),
				new SqlParameter("@CompanyName", SqlDbType.NVarChar, 200),
				new SqlParameter("@Country", SqlDbType.NVarChar, 50),
				new SqlParameter("@Province", SqlDbType.NVarChar, 30),
				new SqlParameter("@City", SqlDbType.NVarChar, 30),
				new SqlParameter("@Address", SqlDbType.NVarChar, 255),
				new SqlParameter("@ZipCode", SqlDbType.NVarChar, 10),
				new SqlParameter("@Phone", SqlDbType.NVarChar, 50),
				new SqlParameter("@Fax", SqlDbType.NVarChar, 30),
				new SqlParameter("@Homepage", SqlDbType.NVarChar, 255),
				new SqlParameter("@BankOfDeposit", SqlDbType.NVarChar, 50),
				new SqlParameter("@BankAccount", SqlDbType.NVarChar, 50),
				new SqlParameter("@TaxNum", SqlDbType.NVarChar, 50),
				new SqlParameter("@StatusInField", SqlDbType.NVarChar, 30),
				new SqlParameter("@CompanySize", SqlDbType.NVarChar, 30),
				new SqlParameter("@BusinessScope", SqlDbType.NVarChar, 200),
				new SqlParameter("@AnnualSales", SqlDbType.NVarChar, 50),
				new SqlParameter("@ManagementForms", SqlDbType.NVarChar, 30),
				new SqlParameter("@RegisteredCapital", SqlDbType.NVarChar, 50),
				new SqlParameter("@CompanyIntro", SqlDbType.NText),
				new SqlParameter("@CompanyPic", SqlDbType.NVarChar, 255),
				new SqlParameter("@CompanyID", SqlDbType.Int, 4)
			};
			array[0].Value = model.ClientID;
			array[1].Value = model.CompanyName;
			array[2].Value = model.Country;
			array[3].Value = model.Province;
			array[4].Value = model.City;
			array[5].Value = model.Address;
			array[6].Value = model.ZipCode;
			array[7].Value = model.Phone;
			array[8].Value = model.Fax;
			array[9].Value = model.Homepage;
			array[10].Value = model.BankOfDeposit;
			array[11].Value = model.BankAccount;
			array[12].Value = model.TaxNum;
			array[13].Value = model.StatusInField;
			array[14].Value = model.CompanySize;
			array[15].Value = model.BusinessScope;
			array[16].Value = model.AnnualSales;
			array[17].Value = model.ManagementForms;
			array[18].Value = model.RegisteredCapital;
			array[19].Value = model.CompanyIntro;
			array[20].Value = model.CompanyPic;
			array[21].Value = model.CompanyID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int CompanyID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientCompany ");
			stringBuilder.Append(" where CompanyID=@CompanyID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CompanyID", SqlDbType.Int, 4)
			};
			array[0].Value = CompanyID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string CompanyIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientCompany ");
			stringBuilder.Append(" where CompanyID in (" + CompanyIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.ClientCompany GetModel(int CompanyID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HL_ClientCompany ");
			stringBuilder.Append(" where CompanyID=@CompanyID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CompanyID", SqlDbType.Int, 4)
			};
			array[0].Value = CompanyID;
			HKB.Data.Model.ClientCompany clientCompany = new HKB.Data.Model.ClientCompany();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.ClientCompany result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.ClientCompany DataRowToModel(DataRow row)
		{
			HKB.Data.Model.ClientCompany clientCompany = new HKB.Data.Model.ClientCompany();
			if (row != null)
			{
				if (row["CompanyID"] != null && row["CompanyID"].ToString() != "")
				{
					clientCompany.CompanyID = int.Parse(row["CompanyID"].ToString());
				}
				if (row["ClientID"] != null && row["ClientID"].ToString() != "")
				{
					clientCompany.ClientID = new int?(int.Parse(row["ClientID"].ToString()));
				}
				if (row["CompanyName"] != null)
				{
					clientCompany.CompanyName = row["CompanyName"].ToString();
				}
				if (row["Country"] != null)
				{
					clientCompany.Country = row["Country"].ToString();
				}
				if (row["Province"] != null)
				{
					clientCompany.Province = row["Province"].ToString();
				}
				if (row["City"] != null)
				{
					clientCompany.City = row["City"].ToString();
				}
				if (row["Districts"] != System.DBNull.Value)
				{
					clientCompany.Districts = row["Districts"].ToString();
				}
				if (row["Address"] != null)
				{
					clientCompany.Address = row["Address"].ToString();
				}
				if (row["ZipCode"] != null)
				{
					clientCompany.ZipCode = row["ZipCode"].ToString();
				}
				if (row["Phone"] != null)
				{
					clientCompany.Phone = row["Phone"].ToString();
				}
				if (row["Fax"] != null)
				{
					clientCompany.Fax = row["Fax"].ToString();
				}
				if (row["Homepage"] != null)
				{
					clientCompany.Homepage = row["Homepage"].ToString();
				}
				if (row["BankOfDeposit"] != null)
				{
					clientCompany.BankOfDeposit = row["BankOfDeposit"].ToString();
				}
				if (row["BankAccount"] != null)
				{
					clientCompany.BankAccount = row["BankAccount"].ToString();
				}
				if (row["TaxNum"] != null)
				{
					clientCompany.TaxNum = row["TaxNum"].ToString();
				}
				if (row["StatusInField"] != null && row["StatusInField"].ToString() != "")
				{
					clientCompany.StatusInField = row["StatusInField"].ToString();
				}
				if (row["CompanySize"] != null && row["CompanySize"].ToString() != "")
				{
					clientCompany.CompanySize = row["CompanySize"].ToString();
				}
				if (row["BusinessScope"] != null)
				{
					clientCompany.BusinessScope = row["BusinessScope"].ToString();
				}
				if (row["AnnualSales"] != null)
				{
					clientCompany.AnnualSales = row["AnnualSales"].ToString();
				}
				if (row["ManagementForms"] != null && row["ManagementForms"].ToString() != "")
				{
					clientCompany.ManagementForms = row["ManagementForms"].ToString();
				}
				if (row["RegisteredCapital"] != null)
				{
					clientCompany.RegisteredCapital = row["RegisteredCapital"].ToString();
				}
				if (row["CompanyIntro"] != null)
				{
					clientCompany.CompanyIntro = row["CompanyIntro"].ToString();
				}
				if (row["CompanyPic"] != null)
				{
					clientCompany.CompanyPic = row["CompanyPic"].ToString();
				}
			}
			return clientCompany;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM HL_ClientCompany ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HL_ClientCompany ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_ClientCompany ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.CompanyID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_ClientCompany T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public HKB.Data.Model.ClientCompany GetCompanyByClientID(int ClientID)
		{
			HKB.Data.Model.ClientCompany result = null;
			string text = "select * from HL_ClientCompany where ClientID=" + ClientID;
			DataSet dataSet = Conn.Query(text.ToString(), new IDbDataParameter[0]);
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			return result;
		}
	}
}
